<template>
  <div id="Cate">
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button @click="showAddCateDialog" type="primary">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table :data="catelist" :columns="columns" :selection-type="false" 
      :expand-type="false" :show-index="true" index-text="#" border class="treeTable">
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i v-if="scope.row.cat_deleted === false" class="el-icon-success" style="color: lightgreen"></i>
          <i v-else class="el-icon-error" style="color: lightgreen"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0" size="mini">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" size="mini" type="success">二级</el-tag>
          <el-tag v-else size="mini" type="warning">三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="scope">
          <el-button @click="showEditDialog(scope.row)" type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button @click="removeCateById(scope.row.cat_id)" type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querInfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
       @close="addCateDialogClosed">
      <!-- 添加分类的表单 -->
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px">
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <!-- options指定数据源 -->
          <!-- props用来指定配置对象 -->
          <el-cascader
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChanged" 
            :clearable="true" >
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑商品分类对话框 -->
    <el-dialog
      title="编辑商品分类"
      :visible.sync="etidCateDialogVisible"
      width="50%"
      @close="editclosed">
      <el-form :model="editCateForm" :rules="editCateFormRules" ref="editCateFormRef" label-width="100px" >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="editCateForm.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="etidCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editOrUpdateate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getHttpCateList, getHttpParentCateList, postHttpAddCate, putHttpEditCateById, deleteHttpDeleteCateById  } from 'network/cate'
import { catnameVf } from 'common/js/validator'
export default {
  name: "Cate",
  data() {
    return {
      //商品分类的数据列表，默认为空
      catelist: [],
      //商品分类数据查询参数
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      //总数据条数
      total: 0,
      //tree-table表格的数量列定义
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          //表示将当前列定义为模板列
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          //表示将当前列定义为模板列
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          //表示将当前列定义为模板列
          type: 'template',
          template: 'opt'
        }
      ],
      //添加分类对话框显示隐藏
      addCateDialogVisible: false,
      //添加分类的表单数据对象
      addCateForm: {
        cat_name: '',
        cat_pid: 0, //父分类的等级
        cat_level: 0 //添加分类的等级 默认是1级分类
      },
      //添加分类表单验证对象
      addCateFormRules: {
        cat_name: catnameVf
      },
      //添加分类对话框父级分类的数据列表
      parentCateList: [],
      //级联菜单的配置对象
      cascaderProps: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true //是否运行选择一级分类
      },
      //级联选择器选中的值
      selectedKeys: [],
      //编辑分类弹窗显示隐藏
      etidCateDialogVisible: false,
      //编辑分类表单
      editCateForm: {
        id: 0,
        cat_name: ''
      },
      //编辑分类表单验证
      editCateFormRules: {
        cat_name:catnameVf
      }
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    getCateList() {
      getHttpCateList(this.querInfo).then(res => {
        if( res.meta.status !== 200) {
          this.$message.error(res.meta.msg)
          return
        }
        else {
          this.catelist = res.data.result
          this.total = res.data.total
        }
      }).catch(err => {
        this.$message.error(err)
      })
    },
    //监听pagesize改变
    handleSizeChange(newSize) {
      this.querInfo.pagesize = newSize
      this.getCateList()
    },
    //监听pagenum改变
    handleCurrentChange(newPage) {
      this.querInfo.pagenum = newPage
      this.getCateList()
    },
    //添加分类对话框关闭事件
    addCateDialogClosed() {
      this.$refs.addCateFormRef.resetFields() 
      this.selectedKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    },
    //添加分类点击事件
    showAddCateDialog() {
      this.addCateDialogVisible = true
      //获取一级二级商品分类
      this.getParentCateList()
    },
    //获取一级二级商品分类
    getParentCateList() {
      getHttpParentCateList(2).then(res => {
        if( res.meta.status !== 200) {
          this.$message.error(res.meta.msg)
          return
        }
        else {
          this.parentCateList = res.data
        }
      }).catch(err => {
        console.log('cateerr')
        this.$message.error(err)
      })
    },
    //级联选择器选中的值发生变化触发的事件
    parentCateChanged() {
      //如果selectedKeys数组中的length大于0 说明选中了父级分类
      //反之，就说明没有选中任何父级分类
      if (this.selectedKeys.length > 0) {
        //父级分类的id
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length -1]
        //父级分类的等级等于数组的长度
        this.addCateForm.cat_level = this.selectedKeys.length
        return
      }
      else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
      // console.log(this.selectedKeys)
    },
    //添加分类确定事件
    addCate() {
      this.$refs.addCateFormRef.validate(valid => {
        if(!valid) {
          return
        }
        else {
          postHttpAddCate(this.addCateForm).then(res => {
            if( res.meta.status !== 201) {
              this.$message.error(res.meta.msg)
              return
            }
            else {
              this.$message.success(res.meta.msg)
              this.getCateList()
              this.addCateDialogVisible = false
            }
          }).catch(err => {
            this.$message.error(err)
          })
        }
      })
      
      //console.log(this.addCateForm)
    },
    //编辑分类对话框关闭事件
    editclosed() {
      this.editCateForm.id = 0
      this.editCateForm.cat_name = ''
    },
    //编辑按钮事件
    showEditDialog(info) {
      this.etidCateDialogVisible = true 
      this.editCateForm.id = info.cat_id
      this.editCateForm.cat_name = info.cat_name
    },
    //编辑确定按钮事件
    editOrUpdateate() {
      //编辑表单验证
      this.$refs.editCateFormRef.validate(valid => {
        if(!valid) {
          return
        }
        else {
          putHttpEditCateById(this.editCateForm.id,this.editCateForm.cat_name).then(res => {
            if( res.meta.status !== 200) {
              this.$message.error(res.meta.msg)
              return
            }
            else {
              this.$message.success(res.meta.msg)
              this.getCateList()
              this.etidCateDialogVisible = false
            }
          }).catch(err => {
            this.$message.error(err)
          })
        }
      })
    },
    //删除分类事件
    removeCateById(id) {
      //弹窗询问用户是否删除数据
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteHttpDeleteCateById(id).then(res => {
            if( res.meta.status !== 200) {
              this.$message.error(res.meta.msg)
              return
            }
            else {
              this.$message({type: 'success', message: '删除成功!' })
              this.getCateList()
            }
          }).catch(err => {
            this.$message.error(err)
          })
          
        }).catch(() => {
          this.$message({ type: 'info',message: '已取消删除' })          
        })
    }
  }
}
</script>

<style lang="less" scoped>
.treeTable {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
